﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;

namespace FineUI.Examples
{
    public partial class admin_arrangecourse : System.Web.UI.Page
    {


        protected DataSet LoadClassInform()
        {
            Maticsoft.BLL.Class currentClass = new Maticsoft.BLL.Class();
            DataSet DSClass = currentClass.GetList("DESC");
            DataView view1 = DSClass.Tables[0].DefaultView;
            GridClass.DataSource = view1;
            GridClass.DataBind();
            return DSClass;
        
        }

        protected DataSet LoadCourseInform() 
        {
            Maticsoft.BLL.Course currentCourse = new Maticsoft.BLL.Course();
            DataSet DSCourse = currentCourse.GetAllList();
            DataView view1 = DSCourse.Tables[0].DefaultView;
            GridCourse.DataSource = view1;
            GridCourse.DataBind();
            return DSCourse;

        
        }

        protected DataSet LoadTeacherInform()
        {
            Maticsoft.BLL.Teacher currentTeacher = new Maticsoft.BLL.Teacher();
            DataSet DSTeacher = currentTeacher.GetAllList();


            DataView view1 = DSTeacher.Tables[0].DefaultView;
            GridTeacher.DataSource = view1;
            GridTeacher.DataBind();
            return DSTeacher;
        
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
            }
            LoadTeacherInform();//加载教师信息
            LoadCourseInform();//加载课程信息
            LoadClassInform();//加载教室信息
        }

       /* protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {

        }

        protected void search_btn_Click(object sender, EventArgs e)
        {

        }*/




        protected void GridCourse_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GridCourse.PageIndex = e.NewPageIndex;
            LoadCourseInform();
        }

        protected void GridClass_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GridClass.PageIndex = e.NewPageIndex;
            LoadClassInform();
        }

        protected void GridTeacher_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GridTeacher.PageIndex = e.NewPageIndex;
            LoadTeacherInform();
        }

        protected void GridCourse_RowCommand(object sender, GridCommandEventArgs e)
        {
            //课程
            if (e.CommandName == "ActionChose")
            { 
                int key=e.RowIndex;
                string Current_C_Id=LoadCourseInform().Tables[0].Rows[key]["C_Id"].ToString();

                tbxCourseName.Text = Current_C_Id;
            
            }


        }

        protected void GridClass_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionChose")
            {
                int key = e.RowIndex;
                string Current_CL_Id = LoadClassInform().Tables[0].Rows[key]["CL_Id"].ToString();

                tbxClassNum.Text = Current_CL_Id;

            }
          
        }

        protected void GridTeacher_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionChose")
            {
                int key = e.RowIndex;
                string Current_T_Name = LoadTeacherInform().Tables[0].Rows[key]["T_Name"].ToString();
                string Current_T_Id = LoadTeacherInform().Tables[0].Rows[key]["T_Id"].ToString();
                Session["Current_T_Id"] = Current_T_Id;
                tbxTeaName.Text = Current_T_Name;

            }
        }

        protected void btnCreateTeaClass_Click(object sender, EventArgs e)
        {
            string currentTeaClassId = tbxTeaClassId.Text.ToString();
            string currentTeaId = "1001";
            if(Session["Current_T_Id"]!=null)
            {
                currentTeaId= Session["Current_T_Id"].ToString();
            }
            
            
            string currentClassNum = tbxClassNum.Text.ToString();
            string currentCourseName = tbxCourseName.Text.ToString();
            string currentTeaClassName=tbxTeaClassName.Text.ToString();
            string currentTeaClassDepartment=tbxTeaClassDepartment.Text.ToString();
            string currentDepartment = tbxTeaClassDepartment.ToString();
            string currentDate_Day = DropDownListWeek.SelectedText.ToString();
            string currentDate_Time = DropDownListTime.SelectedValue.ToString();

            if (currentTeaClassId == "" || currentClassNum == "" || currentCourseName == "" || currentTeaClassDepartment == "" || tbxTeaName.Text == "")
            {
                Alert.ShowInTop("请输入完整的信息", MessageBoxIcon.Information);
                return;
            }

            Maticsoft.BLL.TeachingClass TeachingClass = new Maticsoft.BLL.TeachingClass();

            if (TeachingClass.Exists(currentTeaClassId))
            {
                Alert.ShowInTop("该教学班已经存在", MessageBoxIcon.Information);
                return;
            
            }

            if (TeachingClass.TeacherNoTime(currentTeaId, currentDate_Day, currentDate_Time))
            {
                Alert.ShowInTop("该教师在此时间已经安排其它课程", MessageBoxIcon.Information);
                return;
            
            }
            if (TeachingClass.ClassNoFree(currentClassNum,currentDate_Day, currentDate_Time))
            {
                Alert.ShowInTop("教室在此时间已经安排其它课程", MessageBoxIcon.Information);
                return;     
            }

            Maticsoft.Model.TeachingClass TeachingClasInform = new Maticsoft.Model.TeachingClass();
            TeachingClasInform.TC_Id = currentTeaClassId;
            TeachingClasInform.C_Id = currentCourseName;
            TeachingClasInform.T_Id = currentTeaId;
            TeachingClasInform.CL_Id = currentClassNum;
            TeachingClasInform.TC_Name = currentTeaClassName;
            TeachingClasInform.TC_Department = currentTeaClassDepartment;
            TeachingClasInform.TC_Date_Day = currentDate_Day;
            TeachingClasInform.TC_Date_Time = currentDate_Time;

            if(TeachingClass.Add(TeachingClasInform))
            {
                Alert.ShowInTop("创建教学班成功", MessageBoxIcon.Information);
            
            }
        }
    }
}